﻿using System;
using System.Web.Mvc;
using System.Xml.Linq;
using DotNetBurner.Core;
using DotNetBurner.Web.Settings;

namespace DotNetBurner
{
    public static class OpenSearchHelper
    {
        public static string GetOpenSearchDescription(UrlHelper url)
        {
            XNamespace ns = "http://a9.com/-/spec/opensearch/1.1/";

            XElement openSearch = new XElement(ns + "OpenSearchDescription",
                                               new XAttribute("xmlns", ns.ToString()),
                                               new XAttribute(XNamespace.Xmlns + "moz", "http://www.mozilla.org/2006/browser/search/"),
                                               new XElement(ns + "ShortName", SiteSettings.Settings.Name),
                                               new XElement(ns + "Description", SiteSettings.Settings.Content));

            string siteUrl = SiteSettings.Settings.Address;
            string searchUrl = url.RouteUrl("Search");

            openSearch.Add(new XElement(ns + "Url",
                                        new XAttribute("type", "text/html"),
                                        new XAttribute("template", String.Concat("{0}{1}".FormatWith(siteUrl, searchUrl), "?q={searchTerms}"))));


            openSearch.Add(new XElement(ns + "Image", new XAttribute("height", "16"), new XAttribute("width", "16"), 
                                        new XAttribute("type", "image/x-icon"), 
                                        String.Concat(siteUrl, "/Content/Images/favicon.ico")));

            XDocument doc = new XDocument();
            doc.Add(openSearch);

            return doc.ToString(SaveOptions.DisableFormatting);
        }
    }
}